package com.ruoyi.web.controller.bus;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.ruoyi.bus.domain.WitBusQrCode;
import com.ruoyi.bus.qiniu.QiniuCloudUtil;
import com.ruoyi.bus.service.IWitBusQrCodeService;
import com.ruoyi.bus.wechat.WechatQrCodeUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.WitBus;
import com.ruoyi.bus.service.IWitBusService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆Controller
 * 
 * @author caiyanshan
 * @date 2021-04-14
 */
@RestController
@RequestMapping("/bus/car")
public class WitBusController extends BaseController
{
    @Autowired
    private IWitBusService witBusService;

    @Autowired
    private IWitBusQrCodeService busQrCodeService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询车辆列表
     */
    @PreAuthorize("@ss.hasPermi('bus:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(WitBus witBus)
    {
        startPage();
        List<WitBus> list = witBusService.selectWitBusList(witBus);
        return getDataTable(list);
    }

    /**
     * 导出车辆列表
     */
    @PreAuthorize("@ss.hasPermi('bus:car:export')")
    @Log(title = "车辆", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WitBus witBus)
    {
        List<WitBus> list = witBusService.selectWitBusList(witBus);
        for(WitBus bus : list){
            if(bus.getBusStatus() == 0){
                bus.setBusStatusName("正常");
            }
            if(bus.getBusStatus() == 1){
                bus.setBusStatusName("停用");
            }
        }

        ExcelUtil<WitBus> util = new ExcelUtil<WitBus>(WitBus.class);
        return util.exportExcel(list, "car");
    }

    /**
     * 获取qrCode二维码
     * @param busId
     * @return
     */
    @GetMapping("getBusQrCode")
    public AjaxResult getBusQrCode(@RequestParam Long busId) throws IOException {
        WitBusQrCode busQrCode = new WitBusQrCode();
        busQrCode.setBusId(busId);
        List<WitBusQrCode> witBusQrCodeList = busQrCodeService.selectWitBusQrCodeList(busQrCode);
        if(witBusQrCodeList.size() == 0){//微信获取,用用户端信息获取小程序二维码
            InputStream in = WechatQrCodeUtil.getBusQRImage(busId);
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            MultipartFile multipartFile = new MockMultipartFile("file","file", "image/jpeg", in);
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, multipartFile);
            String url = serverConfig.getUrl() + fileName;
            //上传二维码
            //JSONObject json = QiniuCloudUtil.upload(in, null);
            //busQrCode.setQrCodeUrl(json.getString("key"));
            busQrCode.setQrCodeUrl(url);
            busQrCodeService.insertWitBusQrCode(busQrCode);
        }else{
            busQrCode = witBusQrCodeList.get(0);
        }
        return AjaxResult.success(busQrCode.getQrCodeUrl());
    }

    /**
     * 获取车辆详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:car:query')")
    @GetMapping(value = "/{busId}")
    public AjaxResult getInfo(@PathVariable("busId") Long busId)
    {
        return AjaxResult.success(witBusService.selectWitBusById(busId));
    }

    /**
     * 新增车辆
     */
    @PreAuthorize("@ss.hasPermi('bus:car:add')")
    @Log(title = "车辆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WitBus witBus)
    {
        return toAjax(witBusService.insertWitBus(witBus));
    }

    /**
     * 修改车辆
     */
    @PreAuthorize("@ss.hasPermi('bus:car:edit')")
    @Log(title = "车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WitBus witBus)
    {
        return toAjax(witBusService.updateWitBus(witBus));
    }

    /**
     * 删除车辆
     */
    @PreAuthorize("@ss.hasPermi('bus:car:remove')")
    @Log(title = "车辆", businessType = BusinessType.DELETE)
	  @DeleteMapping("/{busIds}")
    public AjaxResult remove(@PathVariable Long[] busIds)
    {
        return toAjax(witBusService.deleteWitBusByIds(busIds));
    }
}
